package com.xrkj.b2c.controller.sys;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import com.xrkj.b2c.controller.common.BasicController;
import com.xrkj.b2c.model.sys.SysAdmin;
import com.xrkj.b2c.model.sys.SysMenu;
import com.xrkj.b2c.model.sys.SysRole;
import com.xrkj.b2c.service.sys.SysAdminService;
import com.xrkj.b2c.service.sys.SysRoleService;
import com.xrkj.b2c.utils.MD5Util;
import com.xrkj.b2c.utils.StringUtil;
import com.xrkj.b2c.utils.UUIDUtil;
import com.xrkj.b2c.utils.UploadFileUtils;

/**
 * 
 * <B>系统名称：</B><BR>
 * <B>模块名称：</B><BR>
 * <B>中文类名：</B>管理员控制器<BR>
 * <B>概要说明：</B><BR>
 * 
 * 
 */
@RequestMapping("/sysAdmin")
@Controller
public class SysAdminController extends BasicController {
    
    @Autowired
    protected SysAdminService adminService;
    @Autowired
    private SysRoleService roleService;
    
	private Logger logger=Logger.getLogger(SysAdminController.class);
    /**f
     * 
     * <B>方法名称：</B>转向系统用户管理界面<BR>
     * <B>概要说明：</B><BR>
     * @author perry
     * @since 2017年10月23日 	
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/forwardSysAdmin.do")
    public ModelAndView forwardLogin(HttpServletRequest request, HttpServletResponse response) {
        return new ModelAndView("WEB-INF/pages/sys/sys-admin/admin-to-manager");
    }
    
    /**
     * 
     * <B>方法名称：</B>用户列表<BR>
     * <B>概要说明：</B><BR>
     * @author perry
     * @since 2017年11月30日 	
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/adminList")
    @ResponseBody
    public Object adminList(HttpServletRequest request, HttpServletResponse response) {
        String search = request.getParameter("search");//名称
        String offset = request.getParameter("offset");
        String limit = request.getParameter("limit");
        Map<String, Object> ret = new HashMap<String, Object>();
        Map<String, Object> paramter = new HashMap<String, Object>();
        paramter.put("offset", StringUtil.isEmpty(offset) ? null : Integer.parseInt(offset));
        paramter.put("limit", StringUtil.isEmpty(limit) ? null : Integer.parseInt(limit));
        paramter.put("search", search);
        Integer total = adminService.findAdminTotal(paramter);
        List<SysAdmin> menus = new ArrayList<SysAdmin>();
        if (total > 0) {
            menus = adminService.findAdminList(paramter);
        }
        ret.put("total", total);
        ret.put("rows", menus);
        return ret;
    }
    
    /**
     * 
     * <B>方法名称：</B>转向新增方法<BR>
     * <B>概要说明：</B><BR>
     * 
     * @author perry
     * @since 2017年10月20日
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/forwardAddAdmin")
    public ModelAndView forwardAddAdmin(HttpServletRequest request, HttpServletResponse response) {
        //查询权限列表
        Map<String, Object> paramter = new HashMap<String, Object>();
        paramter.put("rStatus", 1);
        List<SysRole> roles = roleService.findRoleList(paramter);
        request.setAttribute("roles", roles);
        return new ModelAndView("WEB-INF/pages/sys/sys-admin/add-admin-dialog");
    }

    /**
     * 
     * <B>方法名称：</B>新增<BR>
     * <B>概要说明：</B><BR>
     * 
     * @author perry
     * @since 2017年11月6日
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/addAdmin")
    @ResponseBody
    public Map<String, Object> addAdmin(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> ret = new HashMap<String, Object>();
        String aAccount = request.getParameter("aAccount");
        String aPwd = request.getParameter("aPwd");
        String aNicheng = request.getParameter("aNicheng");
        String aStatus = request.getParameter("aStatus");
        String aEmail = request.getParameter("aEmail");
        String aRemark = request.getParameter("aRemark");
        String aRoleId = request.getParameter("aRoleId");
        MultipartHttpServletRequest multiRequest=(MultipartHttpServletRequest)request;
        MultipartFile aHeadImg = multiRequest.getFile("aHeadImg");//头像
        
        if (StringUtil.isEmpty(aAccount)) {
            ret.put("success", false);
            ret.put("msg", "保存失败，请输入登录账号");
            return ret;
        }
        if (StringUtil.isEmpty(aPwd)) {
            ret.put("success", false);
            ret.put("msg", "保存失败，请输入登录密码");
            return ret;
        }
        SysAdmin admin = new SysAdmin();
        admin.setaId(UUIDUtil.id());
        admin.setaAccount(aAccount);
        admin.setaPwd(MD5Util.Md5(aPwd));
        admin.setaNicheng(aNicheng);
        admin.setaStatus(StringUtil.isEmpty(aStatus)?0:Short.parseShort(aStatus));
        admin.setaEmail(aEmail);
        admin.setaRemark(aRemark);
        admin.setaRoleId(aRoleId);
        admin.setaCreateTime(new Timestamp(System.currentTimeMillis()));
        if(aHeadImg!=null && aHeadImg.getSize()>0){
            String imgResult = UploadFileUtils.uploadFile("upload", aHeadImg, request);
            if(!StringUtil.isEmpty(imgResult)){
                admin.setaHeadImg(imgResult);
            }
        }
        if ((adminService.insert(admin) > 0)) {
            ret.put("success", true);
            ret.put("msg", "保存成功。");
        } else {
            ret.put("success", false);
            ret.put("msg", "保存失败。");
        }
        return ret;
    }
    
    /**
     * 
     * <B>方法名称：</B>跳转修改页面<BR>
     * <B>概要说明：</B><BR>
     * 
     * @author perry
     * @since 2017年11月6日
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/forwardUpdateAdmin")
    public ModelAndView forwardUpdateAdmin(HttpServletRequest request, HttpServletResponse response) {
        String id = request.getParameter("id");//编号
        SysAdmin admin = adminService.selectByPrimaryKey(id);
        request.setAttribute("item", admin);
        //查询权限列表
        Map<String, Object> paramter = new HashMap<String, Object>();
        paramter.put("rStatus", 1);
        List<SysRole> roles = roleService.findRoleList(paramter);
        request.setAttribute("roles", roles);
        return new ModelAndView("WEB-INF/pages/sys/sys-admin/update-admin-dialog");
    }
    
    /**
     * 
     * <B>方法名称：</B>修改<BR>
     * <B>概要说明：</B><BR>
     * @author perry
     * @since 2017年12月1日 	
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/updateAdmin")
    @ResponseBody
    public Map<String, Object> updateAdmin(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> ret = new HashMap<String, Object>();
        String aId = request.getParameter("aId");
        String aAccount = request.getParameter("aAccount");
        String aPwd = request.getParameter("aPwd");
        String aNicheng = request.getParameter("aNicheng");
        String aStatus = request.getParameter("aStatus");
        String aEmail = request.getParameter("aEmail");
        String aRemark = request.getParameter("aRemark");
        String aRoleId = request.getParameter("aRoleId");
        MultipartHttpServletRequest multiRequest=(MultipartHttpServletRequest)request;
        MultipartFile aHeadImg = multiRequest.getFile("aHeadImg");//头像

        if (StringUtil.isEmpty(aAccount)) {
            ret.put("success", false);
            ret.put("msg", "保存失败，请输入登录账号");
            return ret;
        }
        if (StringUtil.isEmpty(aPwd)) {
            ret.put("success", false);
            ret.put("msg", "保存失败，请输入登录密码");
            return ret;
        }
        
        SysAdmin admin = adminService.selectByPrimaryKey(aId);
        if(admin==null){
            ret.put("success", false);
            ret.put("msg", "修改失败，修改对象为空。");
            return ret;
        }
        
        if(!aPwd.equals(admin.getaPwd())){
            //密码需要修改
            admin.setaPwd(MD5Util.Md5(aPwd));
        }
        admin.setaAccount(aAccount);
        admin.setaNicheng(aNicheng);
        admin.setaStatus(StringUtil.isEmpty(aStatus)?0:Short.parseShort(aStatus));
        admin.setaEmail(aEmail);
        admin.setaRemark(aRemark);
        admin.setaRoleId(aRoleId);
        if(aHeadImg!=null && aHeadImg.getSize()>0){
            String imgResult = UploadFileUtils.uploadFile("upload", aHeadImg, request);
            if(!StringUtil.isEmpty(imgResult)){
                admin.setaHeadImg(imgResult);
            }
        }
        
        if ((adminService.updateByPrimaryKeySelective(admin)) > 0) {
            ret.put("success", true);
            ret.put("msg", "保存成功。");
        } else {
            ret.put("success", false);
            ret.put("msg", "保存失败。");
        }
        return ret;
        
    }
    
    /**
     * 
     * <B>方法名称：</B>删除<BR>
     * <B>概要说明：</B><BR>
     * @author perry
     * @since 2017年12月1日 	
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/deleteAdmin")
    @ResponseBody
    public Map<String, Object> deleteAdmin(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> ret = new HashMap<String, Object>();
        String id = request.getParameter("id");
        if (adminService.deleteByPrimaryKey(id) > 0) {
            //删除成功
            ret.put("success", true);
            ret.put("msg", "删除成功。");
        } else {
            //删除失败
            ret.put("success", false);
            ret.put("msg", "删除失败");
        }
        return ret;
    }
    
}
